package edu.nepu.flink.api.combinStream;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * @Date 2024/2/28 22:37
 * @Created by chenshuaijun
 */
public class ConnectOperator {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Integer> streamSource1 = env.fromElements(1, 2, 3, 4);
        DataStreamSource<String> streamSource2 = env.fromElements("a","b","c");

        /**
         * connect连接流操作需要注意的地方
         * （1）connect一次只能连接两条流
         * （2）这两条流的类型可以是不相同的
         */

        ConnectedStreams<Integer, String> connectStream = streamSource1.connect(streamSource2);

        /**
         * connect连接之后的流我们仍旧是可以使用map和flatMap进行数据的处理，但是每个流有不同的处理逻辑
         */

        SingleOutputStreamOperator<String> mapStream = connectStream.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return String.valueOf(value);
            }

            @Override
            public String map2(String value) throws Exception {
                return value;
            }
        });

        mapStream.print();

        env.execute();

    }
}
